package com.mxchip.livestarmobile.mobile

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.state.AppConstant
import com.orhanobut.hawk.Hawk
import com.tbruyelle.rxpermissions2.RxPermissions
import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle
import com.trello.rxlifecycle3.LifecycleProvider

class WelcomeActivity : AppCompatActivity() {

    private val provider: LifecycleProvider<Lifecycle.Event> = AndroidLifecycle.createLifecycleProvider(this)

    var array = arrayOf(
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS,
            Manifest.permission.READ_CALL_LOG,
            Manifest.permission.WRITE_CALL_LOG,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_SMS,
            Manifest.permission.READ_PHONE_STATE /*,
            Manifest.permission.READ_PHONE_NUMBERS*/)

    //查看权限
    private val permisssion: Unit
        get() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
                    turnToMainActivity()
                } else {
                    showPermissionTip()
                }
            }
        }

    private fun turnToMainActivity() {
        jumpMainActivity()
        finish()
    }

    private fun jumpMainActivity() {

        val hasToken = Hawk.contains(AppConstant.SP_LOGIN_TOKEN)

        if(!hasToken) {
            startActivity(Intent(this, LoginActivity::class.java))
        }
        else {
            startActivity(Intent(this, MainActivity::class.java))
        }

    }

    @SuppressLint("CheckResult")
    private fun showPermissionTip() {

        RxPermissions(this).request(*array).compose(provider.bindToLifecycle())
                .subscribe({ granted ->
                    if (granted) {
                        setResult(PERMISSIONS_GRANTED)
                        turnToMainActivity()
                    } else {
                        setResult(PERMISSIONS_DENIED)
                        finish()
                    }
                }) { error -> Log.e(TAG, "showPermissionTip: $error") }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_permission)
        permisssion
    }

    companion object {
        private const val TAG = "WelcomeActivity"
        private const val PERMISSIONS_DENIED = 1
        private const val PERMISSIONS_GRANTED = 0
    }
}